package com.yangyang.rpc.core.netcom.netty.client;

import com.yangyang.rpc.core.netcom.common.bean.RpcCallbackFuture;
import com.yangyang.rpc.core.netcom.common.bean.RpcResponse;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by chenshunyang on 2016/11/22.
 */
public class NettyClientHandler extends SimpleChannelInboundHandler<RpcResponse> {
    private static final Logger logger = LoggerFactory.getLogger(NettyClientHandler.class);

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, RpcResponse response) throws Exception {

        RpcCallbackFuture future = RpcCallbackFuture.futurePool.get(response.getRequestId());
        future.setResponse(response);
        RpcCallbackFuture.futurePool.put(response.getRequestId(), future);

    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        logger.error(">>>>>>>>>>> yy-rpc netty client caught exception", cause);
        ctx.close();
    }

}
